<template>
  <div id="EditPassWord">
    <div style="font-weight: bold; margin: 20px 0">修改密码</div>
    <el-form
      :hide-required-asterisk="true"
      ref="ruleFormRef"
      :rules="rules"
      label-position="right"
      label-width="auto"
      :model="formLabelAlign"
      style="max-width: 600px"
    >
      <el-form-item label="原密码：" prop="password_old">
        <el-input
          v-model="formLabelAlign.password_old"
          type="password"
          clearable
          placeholder="请输入原密码"
        />
      </el-form-item>
      <el-form-item label="新密码：" prop="password_new">
        <el-input
          v-model="formLabelAlign.password_new"
          type="password"
          clearable
          placeholder="设置密码，长度6-16个字符"
        />
      </el-form-item>
      <el-form-item label="确认密码：" prop="password">
        <el-input
          v-model="formLabelAlign.password"
          type="password"
          clearable
          placeholder="请再次输入密码"
        />
      </el-form-item>
      <el-form-item>
        <div class="btns-box">
          <el-button class="btn" @click="handleSubmit">确认修改</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { getEditPwd } from '@/api/user';
import { ElMessage } from 'element-plus';
const ruleFormRef = ref(null);
import { removeToken } from '@/utils/auth';
import { useStore } from 'vuex';
const router = useRouter();
import { useRouter } from 'vue-router';
const formLabelAlign = reactive({
  phone: '',
  password_new: '',
  password_old: '',
  password: '',
});
const store = useStore();
const validateConfirmPassword = (rule, value, callback) => {
  if (value !== formLabelAlign.password_new) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};
const validatePasswordLength = (rule, val, callback) => {
  if (val.length < 6) {
    callback(new Error('密码长度不足6个字符'));
  } else if (val.length > 16) {
    callback(new Error('密码长度超出16个字符'));
  } else {
    callback();
  }
};
const rules = reactive({
  password_old: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  password_new: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { required: true, validator: validatePasswordLength },
  ],
  password: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { required: true, validator: validateConfirmPassword },
  ],
});
const handleEditPwd = () => {
  formLabelAlign.phone = store.getters.user.userInfo.phone;
  let { ...data } = formLabelAlign;
  getEditPwd(data).then((res) => {
    if (res.data.code == 20000) {
      ElMessage({
        message: res.data.msg,
        type: 'success',
      });
      removeToken();
      store.dispatch('user/changeUserInfo', {});
      router.push('/login');
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const handleSubmit = () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      handleEditPwd();
    } else {
      console.log('error submit!');
      return false;
    }
  });
};
</script>

<style lang="scss" scoped>
.btns-box {
  width: 400px;
  .btn {
    height: 40px;
    width: 100%;
    margin-left: 100px;
    background-color: var(--background-color);
    color: var(--text-color);
  }
}
</style>
